$(document).ready(function() {   

	$('.individualnotes').dblclick(function() {    
	
		
		$('#accordion_cont').toggle(700);
	
	} );


	$('.individualfolder').click(function() { 
		$('#current_ids2').html(this.id); 
	} ); 
	
	
	$('.individualnotesmore').click(function() {   
	
		morefolders();
		
	} );

	$('.individualnotes').click(function(e) {
		
		//alert(e.pageX + '    ' + e.pageY);
		$('#accordion_cont').css('top', e.pageY - 70 );
		//var xx = tinyMCE.activeEditor.getContent({format : 'raw'});
		tinyMCE.activeEditor.setContent('<p>Loading, please wait ...</p>');
		var paramval = this.id;
		
		
		
		$('#current_ids').html(this.id);
		
	$.post("../manager/getContent.cfm", { 'argid': paramval })  
	    .done(function(data) {
		
			var xx = data.toString();
		
		        var startposition = xx.indexOf('~trimstartshere2~');
				var startposition = parseInt(startposition);
				var endposition   = xx.indexOf('~trimendshere2~'); 
				var endposition   = parseInt(endposition);	
			  
					if (startposition >= 0 && endposition >= 0)
					{
					   var answered = xx.substring(startposition + 17, endposition);
					   tinyMCE.activeEditor.setContent(answered);
					   
					}
					else
					{ 
						
						tinyMCE.activeEditor.setContent('Unable to load data.');
						return false;
					}
		
		})
		.fail(function(event, jqxhr, settings, exception) { 
		
			  alert(settings);
			  
			  return false;
		
		 });
		
		
		
		//alert(xx);
		
		
		});
		
		
		$('.individualnotesadd').click(function() { 
		
			
			var notename = prompt('Enter topic name:', '');
			
			
			if(notename != null && notename.length > 0)
			{
				var current_id = this.id.toString();
				var current_id = current_id + '~' + notename;
				//
				$.post("../manager/addtopic.cfm", { 'argcurrentid': current_id, 'thiscontent': 'empty string' })  
	              	.done(function(data) {
					  
					  	window.location.reload();
					  
					  })
					.fail(function(event, jqxhr, settings, exception) { 
					
						  alert(settings);
						  
						  return false;
					
					 });
				
			}
			else
			{
				return false;	
			}
			
					
		});
		
		
		$('.individualnotesdelete').click(function() { 
		
			
			var notename = confirm('Are you sure you want to delete this topic?');
			
			
			if(notename == true)
			{
				
				var current_id = $('#current_ids').html();
				var current_id = current_id + '~' + notename;
				//
				$.post("../manager/deletetopic.cfm", { 'argcurrentid': current_id, 'thiscontent': 'empty string' })  
	              	.done(function(data) {
					  
					  	window.location.reload();
					  
					  })
					.fail(function(event, jqxhr, settings, exception) { 
					
						  alert(settings);
						  
						  return false;
					
					 });
				
			}
			else
			{
				return false;	
			}
			
					
		});
		   
		$('.individualnotesxfolder').click(function() { 
		
			
			var notename = confirm('Are you sure you want to delete the current folder?');
			
			
			if(notename == true)
			{
				
				var current_id = $('#current_ids2').html();
				var current_id = current_id + '~' + notename;
				//
				$.post("../manager/deletefolder.cfm", { 'argcurrentid': current_id, 'thiscontent': 'empty string' })  
	              	.done(function(data) {
					  
					  	window.location.reload();
					  
					  })
					.fail(function(event, jqxhr, settings, exception) { 
					
						  alert(settings);
						  
						  return false;
					
					 });
				
			}
			else
			{
				return false;	
			}
			
					
		}); 
		
		$('.individualnotesaddfolder').click(function() { 
		
			
			var notename = prompt('Enter folder name:', '');
			
			
			if(notename != null && notename.length > 0)
			{
				
				var current_id = $('#current_ids2').html();
				var current_id = current_id + '~' + notename;
				//
				$.post("../manager/addfolder.cfm", { 'argcurrentid': current_id, 'thiscontent': 'empty string' })  
	              	.done(function(data) {
					  
					  	window.location.reload();
					  
					  })
					.fail(function(event, jqxhr, settings, exception) { 
					
						  alert(settings);
						  
						  return false;
					
					 });
				
			}
			else
			{
				return false;	
			}
			
					
		});

});
function savenow()
{
	
	
	
	var current_id = $('#current_ids').html();
	
	
	
	var thiscontentss = tinyMCE.activeEditor.getContent({format : 'raw'});
	//var thiscontentss = tinyMCE.activeEditor.getContent();
	
	
	$.post("../manager/savetopic.cfm", { 'argcurrentid': current_id, 'thiscontent': thiscontentss  })  
	    .done(function(data) {  
		
			//Success
			//alert('Successfully saved.');  
			$('#accordion_cont').hide(400);
			$('#accordion_cont').show(400);  
		
		})
		.fail(function(event, jqxhr, settings, exception) { 
		
			  //alert(settings + 'error');
			  
			  return false;
		
		 });
	
	
	return true;
	
}

function morefolders()
{
	
	
	$.post("../manager/more.cfm", { 'argid': 'no' })  
		.done(function(data) {
		
			var xx = data.toString();
		
				var startposition = xx.indexOf('~smore~');
				var startposition = parseInt(startposition);
				var endposition   = xx.indexOf('~emore~'); 
				var endposition   = parseInt(endposition);	
			  
					if (startposition >= 0 && endposition >= 0)
					{
					   var answered = xx.substring(startposition + 7, endposition);
					   
					   $('.individualnotesmore').remove();
					   $('.individualnotesmore_more').remove();
					   
					   
					   $('#accordion').append(answered);
					   $('#accordion').append("<h3 onclick='morefolders()' class='individualnotesmore ui-accordion-header ui-helper-reset ui-state-default ui-corner-all ui-accordion-icons' id='more'><a  href='##' style='color: #06F'>More ...</a></h3>");
					   $( "#accordion" ).accordion( 'refresh' );
					   //$( "#accordion" ).accordion( "refresh" );
					   alert('refresh');
					   //$('.questions').accordion("refresh"); 
					   
					}
					else
					{ 
						
						tinyMCE.activeEditor.setContent('Unable to load data.');
						return false;
					}
		
		})
		.fail(function(event, jqxhr, settings, exception) { 
		
			  alert(settings);
			  
			  return false;
		
		 });
		
	
}



function shareclick()
		{
			
			var collabcode = ''; //get the collabcode please...
			var curr_pe = $('#selectionid').val();
			
			var current_collab = $("#current_ids").html();
				var t_arr = new Array();
				t_arr = current_collab.split('~');
				collabcode = t_arr[1];
			
			if ( curr_pe == 'Only Me' )
			{
				$("#okbutton").button("disable");
				
				
					    $.post("../manager/shareonlyme.cfm", { 'argid': current_collab })  
						.done(function(data) {
						 
							$( "#dialog" ).dialog( "close" );
									
						})
						.fail(function(event, jqxhr, settings, exception) { 
						
							  alert(settings);
							  $( "#dialog" ).dialog( "close" );
							  
							  return false;
						
						 });
				
				return true;
			}
			else 
			{
				$("#okbutton").button("disable");
				var contents_len = $('#log div').length;
				
				if ( contents_len < 1 )
				{
					
				 	return false;
						
				}
				
				var tempcont = '';
				
				
				for ( var count = 1; count <= contents_len; count++ )
				{
					var specontent = $('#log div:nth-child('+ count +')').text();
					tempcont = tempcont + '<>' + specontent;
					//alert(specontent);
					
				} //end loop for
				
				//ajax start
					
					    $.post("../manager/share.cfm", { 'content': tempcont, 'argid': collabcode })  
						.done(function(data) {
						
						
							var xx = data.toString();
						
								var startposition = xx.indexOf('~trimstartshere2~');
								var startposition = parseInt(startposition);
								var endposition   = xx.indexOf('~trimendshere2~'); 
								var endposition   = parseInt(endposition);	
								
							  
									if (startposition >= 0 && endposition >= 0)
									{
									   //var answered = xx.substring(startposition + 17, endposition);
									   //alert(answered);
									   //alert('share');
									   $( "#dialog" ).dialog( "close" );
									    
									}
									else
									{ 
										
										alert('Missing delimiter.');
										$( "#dialog" ).dialog( "close" );
										
									}
						
						})
						.fail(function(event, jqxhr, settings, exception) { 
						
							  alert(settings);
							  $( "#dialog" ).dialog( "close" );
							  
						
						 });
				
				
				
				//ajax end
				
				
				return true;
				
			} //end else
			
			
		} //end function
		
		function sharepeoplechange()
		{
			var curr_pe = $('#selectionid').val();
			if ( curr_pe == 'Public' )
			{
				$('#containerp').hide();
			}
			else
			{
				$('#containerp').show();
			}
		}